package com.lottery.system.service.impl;

import com.lottery.system.controller.PrizeController;
import com.lottery.system.controller.param.PageParam;
import com.lottery.system.controller.param.addPrizeParam;
import com.lottery.system.dao.dataobject.PrizeDO;
import com.lottery.system.dao.mapper.PrizeMapper;
import com.lottery.system.service.PictureService;
import com.lottery.system.service.PrizeService;
import com.lottery.system.service.dto.PrizeDTO;
import com.lottery.system.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-06 22:37
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.service.impl
 * description: 奖品业务逻辑层的实现类
 */
@Service
public class PrizeServiceImpl implements PrizeService {

    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    /**
     * @description: 创建奖品
     * @author: xiaoxie
     * @date: 2024/9/6 22:51
     * @param: [param, picFile]
     * @return: java.lang.Long
     **/
    @Override
    public Long addPrize(addPrizeParam param, MultipartFile picFile) {
       //上传图片
        String picFileName = pictureService.savePicture(picFile);
        //构造参数
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setPrice(param.getPrice());
        prizeDO.setImageUrl(picFileName);
        //存表
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    /**
     * @description: 获取奖品列表
     * @author: xiaoxie
     * @date: 2024/9/8 10:11
     * @param: [param]
     * @return: com.lottery.system.service.dto.PrizeListDTO<com.lottery.system.service.dto.PrizeDTO>
     **/
    @Override
    public PageListDTO<PrizeDTO> getPrizeList(PageParam param) {
        //查询总量
        int total = prizeMapper.count();
        //获取奖品列表
        List<PrizeDTO> prizeDTOList = new ArrayList<PrizeDTO>();
        List<PrizeDO> prizeDOList = prizeMapper.getprizeList(param.getOffSet(),param.getPageSize());
        for(PrizeDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setName(prizeDO.getName());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setPrice(prizeDO.getPrice());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTOList.add(prizeDTO);
        }
        //返回
        return  new PageListDTO<PrizeDTO>(total, prizeDTOList);
    }
}
